﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WebView.WSTemplate;
using System.Collections.Generic;

namespace WebView.Template {
	public partial class Default : System.Web.UI.Page {
		private TemplatesServices t;
	
		protected void Page_Load(object sender, EventArgs e) {
			t = new TemplatesServices();
			this.refreshGrid();
		}

		protected void ButtonAdicionar_Click(object sender, EventArgs e) {
			PanelGrid.Visible = false;
			PanelForm.Visible = true;
		}

		protected void ButtonRemover_Click(object sender, EventArgs e) {
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
			t.remover(id);
			this.refreshGrid();
		}

		protected void ButtonConfirmar_Click(object sender, EventArgs e) {
			string saveDir = @"\File\FileTemplates\";
			string appPath = Request.PhysicalApplicationPath;
			if (FileUploadArquivo.HasFile) {
				string savePath = appPath + saveDir + Server.HtmlEncode(FileUploadArquivo.FileName);
				FileUploadArquivo.SaveAs(savePath);
				t.adicionar(TextBoxNome.Text, FileUploadArquivo.FileName);
				PanelGrid.Visible = true;
				PanelForm.Visible = false;
			}
			this.refreshGrid();
		}

		protected void ButtonCancelar_Click(object sender, EventArgs e) {
			PanelGrid.Visible = true;
			PanelForm.Visible = false;
		}
		
		private void refreshGrid() {
			GridView1.DataSource = t.listar();
			GridView1.DataBind();
		}
		
	}
}
